<?php
/**
 * @ClassName: Theme
 * @Description: APP主题
 * @author: wyaohu
 * @Date: 2024-09-14 10:29
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Theme extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\Theme();
        $this->pageTitle = 'APP主题';
        $this->enableField = 'status';
        $this->pagesize = 10;
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;
        $search->text('name', '主题名称');
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (!empty($searchData['name'])) {
            $where[] = ['name', 'like', '%' . $searchData['name'] . '%'];
        }
        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->show('name', '主题名称');
        $table->show('color', '主题颜色');
//        $table->show('color_picker', '背景颜色');
        $table->fields('color_picker', '背景颜色')->with(
            $table->show('color_picker', '背景颜色'),
            $table->raw('color_picker_html', '背景颜色')
        );
        $table->image('bg_pic', '背景图');
//        $table->show('gradient_color', '渐变颜色');
        $table->fields('gradient_color', '渐变颜色')->with(
            $table->show('gradient_color', '渐变颜色'),
            $table->raw('gradient_color_html', '渐变颜色')
        );
        $table->switchBtn('status', '是否使用')->autoPost('setStatus', true);

        $table->fields('create_times', '添加/修改时间')->with(
            $table->show('create_time', '添加时间'),
            $table->show('update_time', '修改时间')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, create_time');

        $table->getToolbar()
            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
            ->btnView();
    }

    /**
     * 修改使用状态
     * @return \think\response\Json|\tpext\think\View
     */
    public function setStatus()
    {
        $id = input('id');
        $state = input('value', 0);
        if (!$id) {
            $this->error('请认真操作');
        }

        if ($state == 1) {
            $this->dataModel->where('status', 1)->update(['status' => 0]);
        } else {
            return $this->builder()->layer()->closeRefresh(1, '必须有一项在使用中');
        }

        $res = $this->dataModel->where('id', $id)->update(['status' => $state]);

        if (!$res) {
            return json(['code' => 0, 'msg' => '操作失败']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $form->text('name', '主题名称')->required();
        $form->text('color', '主题颜色')->required();
        $form->text('color_picker', '背景颜色')->required();
        $form->image('bg_pic', '背景图');
        $form->text('gradient_color', '渐变颜色')->required();
        $form->switchBtn('status', '是否使用')->default(0);

        if ($isEdit) {
            $form->show('create_time', '添加时间');
            $form->show('update_time', '更新时间');
        }
    }

    /**
     * 保存数据
     * @param $id
     * @return mixed|\think\response\Json
     */
    private function save($id = 0)
    {
        $data = request()->only([
            'name',
            'color',
            'color_picker',
            'bg_pic',
            'gradient_color',
            'status'
        ], 'post');

        $result = $this->validate($data, [
            'name|主题名称' => 'require',
            'color|主题颜色' => 'require',
            'color_picker|背景颜色' => 'require',
            'gradient_color|渐变颜色' => 'require',
        ]);

        if (!$result) {
            $this->error($result);
        }

        if ($data['status'] == 1) {
            $this->dataModel->where('status', 1)->update(['status' => 0]);
        } else {
            $theme = $this->dataModel->where('id', $id)->find();
            if ($theme && $theme['status'] == 1) {
                return json(['code' => 0, 'msg' => '必须有一项在使用中']);
            }
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

//        return $this->builder()->layer()->closeRefresh();
        return $this->doSave($data, $id);
    }

}